﻿using HANS.DataBaseTool;
using HANS_BackStage.Model.DbModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HANS.Utility.Extensions;

namespace HANS_BackStage.Impl
{
    public class PermissionServices
    {
        private static HANS_BackStage.DAL.BackStageDataEntityDAL<Backstage_permission> permissionDal = new DAL.BackStageDataEntityDAL<Backstage_permission>();

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static Msg Add(Backstage_permission entity)
        {
            Msg msg = new Msg();
            if (permissionDal.Insert(entity) > 0)
            {
                msg.AddMsg("添加成功！");
            }
            else
            {
                msg.Result = false;
                msg.AddMsg("添加失败！");
            }

            return msg;
        }

        /// <summary>
        /// 根据id获取权限实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Backstage_permission GetEntityById(string id)
        {
            return permissionDal.Load(id);
        }


        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static Msg Update(Backstage_permission entity)
        {
            Msg msg = new Msg();
            string sql = permissionDal.UpdateSQL(entity).ReplaceParentIDNull();
            int result = DbHelper.ExecuteSQLHelper(sql, HANS_Base.HANSDataBaseLink.admin, msg);
            if (result> 0)
            {
                msg.AddMsg("修改成功！");
            }
            else
            {
                msg.Result = false;
                msg.AddMsg("修改失败！");
            }

            return msg;
        }
        /// <summary>
        /// 更改权限的父亲，排序
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static Msg UpdateParentANDOrdernum(Backstage_permission entity) {
            Msg msg = new Msg();
            string sql= permissionDal.UpdateSQL(entity).ReplaceParentIDNull();
            int result = DbHelper.ExecuteSQLHelper(sql, HANS_Base.HANSDataBaseLink.admin, msg);
            return msg;
        }

        /// <summary>
        /// 删除权限
        /// 1、判断entity父权限
        /// entity父权限：为空--子权限的父权限为空； 不为空--子权限的父权限便跟entity的父权限
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static Msg HandleDeletePermission(Backstage_permission entity)
        {
            Msg msg = new Msg();
            List<string> sqlList = new List<string>();

            var father = entity.Parentid;
            var childrens = Role_PermiServices.GetPerChildrenList(entity.Id);
            if (father.IsNotNullAndNotWhiteSpace())
            {
               
                foreach (var item in childrens)
                {
                    item.Parentid = father;
                    string sqlStr = permissionDal.UpdateSQL(item);
                    sqlList.Add(sqlStr);
                }
            }
            else
            {
                foreach (var item in childrens)
                {
                    item.Parentid = null;
                    string sqlStr = string.Format("UPDATE Backstage_permission SET Parentid = NULL WHERE Id = '{0}'", item.Id);
                    sqlList.Add(sqlStr);
                }
            }
            string deleteSql = permissionDal.DeleteSQL(entity.Id);
            sqlList.Add(deleteSql);
            DbHelper.TranscationExecute(sqlList, HANS_Base.HANSDataBaseLink.admin, msg);
            return msg;
        }


      

    }
}
